package cn.autowired.exam.controller;

import cn.autowired.exam.base.CommonResponse;
import com.google.common.collect.Maps;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.mapper.EntityWrapper;

import cn.autowired.exam.service.TeacherService;

import cn.autowired.exam.entity.Teacher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;

/**
 *
 *   @description : Teacher 控制器
 *   ---------------------------------
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController extends AbstractController {
private final Logger logger=LoggerFactory.getLogger(TeacherController.class);

    @Autowired
    public TeacherService teacherService;


    @RequestMapping("/login")
    public ResponseEntity login(String username,String password){
        Teacher teacher = teacherService.selectOne(new EntityWrapper<Teacher>()
                .eq(Teacher.Field.username, username)
                .eq(Teacher.Field.password, password));
        if (null != teacher){
            HashMap<String, Object> userKey = Maps.newHashMap();
            userKey.put("name",teacher.getName());
            userKey.put("delayTime",System.currentTimeMillis()+24*60*60*1000);
            userKey.put("creatTime",System.currentTimeMillis());
            userKey.put("teacherId",teacher.getId());

            return ResponseEntity.ok(userKey);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    @RequestMapping("/edit")
    public ResponseEntity edit(@RequestParam(required = true) Integer teacherId,
                               @RequestParam(required = false) String name,
                               @RequestParam(required = false) String qq,
                               @RequestParam(required = false) String strength,
                               @RequestParam(required = false) String honour,
                               @RequestParam(required = false) String freeTime){
        Teacher teacher = teacherService.selectOne(new EntityWrapper<Teacher>()
                .eq(Teacher.Field.id, teacherId));
        if (null != teacher){
            if (name!=null){
                teacher.setName(name);
            }
            if (freeTime !=null){

                teacher.setFreeTime(freeTime);
            }
            if (honour!=null){

                teacher.setHonour(honour);
            }
            if (strength!=null){

                teacher.setStrength(strength);
            }
            if (qq!=null){
                teacher.setQq(qq);
            }
            boolean b = teacher.updateById();

            if (b){
                return ResponseEntity.ok(CommonResponse.ok());
            }else {
                return ResponseEntity.ok(CommonResponse.error());
            }
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    @RequestMapping("/delete")
    public ResponseEntity delete(Integer id){
        boolean b = teacherService.deleteById(id);
        if (b){
            return ResponseEntity.ok(CommonResponse.ok());
        }else {
            return ResponseEntity.ok(CommonResponse.error());
        }
    }

    /**
     * 多条件查询老师列表
     * @param strength
     * @param honour
     * @param name
     * @param freeTime
     * @return
     */
    @RequestMapping("/getTeacherList")
    public ResponseEntity getTeacherList(@RequestParam(required =false) String strength,
                                         @RequestParam(required =false) String honour,
                                         @RequestParam(required =false) String name,
                                         @RequestParam(required =false) String freeTime){
        List<Teacher> teacherList = teacherService.getTeacherList(strength, honour, name, freeTime);
        for (Teacher teacher : teacherList) {
          teacher.setPassword(null);
        }
        return ResponseEntity.ok(CommonResponse.ok(teacherList));
    }

    /**
     * 注册
     * @param strength
     * @param honour
     * @param name *
     * @param freeTime *
     * @param password *
     * @param username *
     * @param qq *
     * @return
     */
    @RequestMapping("/register")
    public ResponseEntity register(@RequestParam(required =false) String strength,
                                    @RequestParam(required =false) String honour,
                                    String name,
                                    String freeTime,
                                   String password,
                                   String username,
                                   String qq){
        Teacher teacher = new Teacher();
        teacher.setQq(qq);
        teacher.setStrength(strength);
        teacher.setFreeTime(freeTime);
        teacher.setHonour(honour);
        teacher.setUsername(username);
        teacher.setPassword(password);
        teacher.setName(name);
        boolean b = teacher.insertOrUpdate();
        if (b){
            return ResponseEntity.ok(CommonResponse.ok());
        } else {
            return ResponseEntity.ok(CommonResponse.error());
        }
    }


    @RequestMapping("/getTeacherByTeacherId")
    public ResponseEntity getTeacherByTeacherId(Integer teacherId){
        Teacher teacher = teacherService.selectById(teacherId);
        return ResponseEntity.ok(CommonResponse.ok(teacher));
    }

    @RequestMapping("/modify")
    public ResponseEntity modifyPassword(Integer teacherId,String oldPassword,String newPassword){
        Teacher teacher = teacherService.selectOne(new EntityWrapper<Teacher>().eq(Teacher.Field.id, teacherId).eq(Teacher.Field.password, oldPassword));
        if (teacher!=null){
            teacher.setPassword(newPassword);
            teacher.updateById();
        }else {
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok(CommonResponse.ok());
    }
}